﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.CS
{
    public class CSBL
    {
        protected void clsCsInsert(tbl_C css)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_C cs = new tbl_C();
                    cs.CsQuestion = css.CsQuestion;
                    cs.CsAnswer = css.CsAnswer;
                    cs.CsType = css.CsType;
                    cs.UsernameLogin = css.UsernameLogin;
                    cs.CreatedDate = css.CreatedDate;
                    cs.UserReply = css.UserReply;
                    cs.ReplyDate = css.ReplyDate;
                    cs.CreatedDate = DateTime.Now;
                    cs.Status = css.Status;

                    adlerContextDb.tbl_Cs.InsertOnSubmit(cs);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCsModify(tbl_C css)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var cs = adlerContextDb.tbl_Cs.Single(d => d.CsNo == css.CsNo);
                    cs.CsQuestion = css.CsQuestion;
                    cs.CsAnswer = css.CsAnswer;
                    cs.CsType = css.CsType;
                    cs.UsernameLogin = css.UsernameLogin;
                    cs.CreatedDate = css.CreatedDate;
                    cs.UserReply = css.UserReply;
                    cs.ReplyDate = css.ReplyDate;
                    cs.Status = css.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCsDelete(tbl_C css)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var cs = adlerContextDb.tbl_Cs.Single(d => d.CsNo == css.CsNo);
                    cs.Status = css.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Cs.
        public List<MODEL_SP_ADMIN_SEARCH_CS> Get_Admin_Cs(string csquestion, string datefrom, string dateto,
                    string username, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_CS(csquestion, datefrom, dateto, username, status)
                //            select new MODEL_SP_ADMIN_SEARCH_CS
                //            {
                //                Index = index++,
                //                CsNo = n.CsNo,
                //                CsQuestion = n.CsQuestion,
                //                CsAnswer = n.CsAnswer,
                //                CsType = n.CsType,
                //                UsernameLogin = n.UsernameLogin,
                //                CreatedDate = n.CreatedDate,
                //                UserReply = n.UserReply,
                //                ReplyDate = n.ReplyDate,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_C GetCsByCsno(int csno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Cs.Single(d => d.CsNo == csno);
            }
        }
    }
}
